package webCam;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaWebCam.java
 *
 * Created on 07-ene-2012, 19:02:25
 */


import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Jesus Ruiz Oliva
 */
public class VistaWebCam extends javax.swing.JPanel {
    
    PanelDobleBuffer paneldb = new PanelDobleBuffer();
    private Component component = null;
    JPanelImpl jPanelImpl = null;
    /** Creates new form VistaWebCam */
    public VistaWebCam() {
        
        paneldb.setLayout(new BorderLayout());
        paneldb.setAreaX(0);
        paneldb.setAreaY(0);
        paneldb.setAreaHeight(0);
        paneldb.setAreaWidth(0);
        paneldb.setStateButton(0);
        
        initComponents();
        jPanel1.add(paneldb);
        jPanel1.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();

        setLayout(null);

        jButton1.setText("Iniciar");
        add(jButton1);
        jButton1.setBounds(770, 120, 110, 25);

        jButton2.setText("Capturar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2);
        jButton2.setBounds(770, 210, 110, 25);

        jButton3.setText("Guardar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        add(jButton3);
        jButton3.setBounds(770, 300, 110, 25);

        jButton4.setText("Salir");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        add(jButton4);
        jButton4.setBounds(770, 390, 110, 25);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));
        add(jPanel1);
        jPanel1.setBounds(55, 68, 680, 510);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Imagen seleccionada", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));
        add(jPanel2);
        jPanel2.setBounds(930, 70, 220, 290);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
    public void addListener(ActionListener aLis){
        jButton1.addActionListener(aLis);
        jButton2.addActionListener(aLis);
        jButton3.addActionListener(aLis);
        jButton4.addActionListener(aLis); 
    }
    public void addMouseActionListener (MouseListener aMLis){
        component.addMouseListener(aMLis);
    }
    @Override
    public void addMouseMotionListener (MouseMotionListener aMMLis){
        component.addMouseMotionListener(aMMLis);
    }
    public JButton getIniciarCamara(){
        return jButton1;
    }
    public JButton getSeleccionarArea(){
        return jButton2;
    }
    public JButton getGuardar(){
        return jButton3;
    }
    public JButton getAtras(){
        return jButton4;
    }
    public void setStateButton(int i){
        paneldb.setStateButton(i);
    }
    public PanelDobleBuffer getPanelDobleBuffer(){
        return paneldb;
    }
    public JPanel getJPanel1(){
        return jPanel1;
    }
    public JPanel getJPanel2(){
        return jPanel2;
    }
    public void setComponent(Component component){
        this.component = component;
        paneldb.add(component);
    }

    public void mostrarFoto(Image verFoto) {
        int x,y,width,height;
        x = paneldb.getAreaX();
        y = paneldb.getAreaY();
        width = paneldb.getAreaWidth();
        height = paneldb.getAreaHeight();
        if(height < 0 && width  < 0){
            x += width;
            y += height;
        }
        if (height < 0 && width >= 0){
            y += height;
        }
        if (width < 0 && height >= 0){
            x += width;
        }
        width = Math.abs(width);
        height = Math.abs(height);        
        jPanelImpl = new JPanelImpl(createImage(new FilteredImageSource(
                                   verFoto.getSource(),new CropImageFilter(x,y,width,height))));
        jPanel2.add(jPanelImpl);
        jPanel2.updateUI();
    }
    public JPanelImpl getJPanelImpl(){
        return jPanelImpl;
    }
    public  class JPanelImpl extends JPanel {
        Image im;
        public JPanelImpl(Image im) {
            this.im = im;
        }
        @Override
        public void paint(Graphics g){
            if (im != null)   
            g.drawImage(im,0, 0, im.getWidth(null), im.getHeight(null),this);            
            setOpaque(false);
            super.paintComponent(g) ;   
        }
        public Image getImagen(){
            return im;
        }
    }
}
